package com.lewei.view;

import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.io.File;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JSeparator;

import com.lewei.dao.SqliteDao;
import com.lewei.model.Adjustment;
import com.lewei.utils.Constants;
import com.lewei.utils.ExcelFileFilter;

/**
 * 调整读取Excel路径。
 * 
 */
public class AdjustPathPositionView extends JFrame {
	private static final long serialVersionUID = -7274259983815728490L;
	private static AdjustPathPositionView alp = null;
	private static JTextField _OL100_path;
	private static JTextField certificate_path;
	private static JTextField rule_path;
	/** 文件选择器 */
	JFileChooser jfc = new JFileChooser();
	private static JTextField _OL35_path;
	private static JTextField _OL25_path;
	private static JTextField _SGB_path;
	private static JTextField _SGB05_path;
	private static JTextField _OL35SCHINDLER_path;

	public static void main(String[] args) {
		alp = new AdjustPathPositionView();
		alp.setVisible(true);
	}

	public static AdjustPathPositionView GetAdjustLabelPositionView() {
		if (alp == null) {
			alp = new AdjustPathPositionView();
		}
		// 更新标签信息。
		try {
			updatePosition();
			// updateFontSize();
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.getMessage());
		}
		return alp;
	}

	private AdjustPathPositionView() {
		double screenWidth = Toolkit.getDefaultToolkit().getScreenSize()
				.getWidth();
		double screenHeight = Toolkit.getDefaultToolkit().getScreenSize()
				.getHeight();
		setBounds((int) ((screenWidth - 400) / 2),
				(int) ((screenHeight - 295) / 2), 400, 352);
		// 不可改变窗口大小
		setResizable(true);
		// 设置图标
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				MainPrint.class.getResource("/images/Wittur_Logo.gif")));
		// 设置标题
		setTitle("标签调整");
		getContentPane().setLayout(null);

		JLabel label = new JLabel("\u8DEF\u5F84");// 路径
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("SimSun", Font.PLAIN, 12));
		label.setBounds(151, 10, 65, 15);
		getContentPane().add(label);

		JLabel label_1 = new JLabel("\u8BBE\u7F6E");// 设置
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setFont(new Font("SimSun", Font.PLAIN, 12));
		label_1.setBounds(289, 10, 66, 15);
		getContentPane().add(label_1);

		JButton button = new JButton("\u63D0\u4EA4");// 提交
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				try {
					String _OL100_path_str = _OL100_path.getText().toString()
							.trim().isEmpty() ? "0" : _OL100_path.getText()
							.toString();
					String _OL35_path_str = _OL35_path.getText().toString()
							.trim().isEmpty() ? "0" : _OL35_path.getText()
							.toString();

					String _OL35SCHINDLER_path_str = _OL35SCHINDLER_path
							.getText().toString().trim().isEmpty() ? "0"
							: _OL35SCHINDLER_path.getText().toString();
					String _SGB05_path_str = _SGB05_path.getText().toString()
							.trim().isEmpty() ? "0" : _SGB05_path.getText()
							.toString();
					String _SGB_path_str = _SGB_path.getText().toString()
							.trim().isEmpty() ? "0" : _SGB_path.getText()
							.toString();
					String certificate_path_str = certificate_path.getText()
							.toString().trim().isEmpty() ? "0"
							: certificate_path.getText().toString();
					String rule_path_str = rule_path.getText().toString()
							.trim().isEmpty() ? "0" : rule_path.getText()
							.toString();
					SqliteDao sd = new SqliteDao();
					if (!sd.setPath("OL100", _OL100_path_str)) {
						JOptionPane
								.showMessageDialog(null, "无法更新标签OL100的路径数据！");
						return;
					}
					if (!sd.setPath("OL35", _OL35_path_str)) {
						JOptionPane.showMessageDialog(null, "无法更新标签OL35的路径数据！");
						return;
					}
					if (!sd.setPath("OL35SCHINDLER", _OL35SCHINDLER_path_str)) {
						JOptionPane.showMessageDialog(null,
								"无法更新标签OL35SCHINDLER的路径数据！");
						return;
					}
					if (!sd.setPath("SGB05", _SGB05_path_str)) {
						JOptionPane
								.showMessageDialog(null, "无法更新标签SGB05的路径数据！");
						return;
					}
					if (!sd.setPath("SGB", _SGB_path_str)) {
						JOptionPane.showMessageDialog(null, "无法更新标签SGB的路径数据！");
						return;
					}
					if (!sd.setPath("certificate", certificate_path_str)) {
						JOptionPane.showMessageDialog(null, "无法更新标签证书的路径数据！");
						return;
					}
					if (!sd.setPath("rule", rule_path_str)) {
						JOptionPane.showMessageDialog(null, "无法更新标签规则的路径数据！");
						return;
					}

					JOptionPane.showMessageDialog(null, "标签的路径数据已经更新！");
					dispose();//关闭
				} catch (Exception e2) {
					e2.printStackTrace();
					JOptionPane.showMessageDialog(null, e2.getMessage());
				}
			}
		});
		button.setBounds(167, 281, 65, 23);
		getContentPane().add(button);

		JLabel label_4 = new JLabel("\u540D  \u79F0");// 名称
		label_4.setHorizontalAlignment(SwingConstants.CENTER);
		label_4.setFont(new Font("宋体", Font.PLAIN, 12));
		label_4.setBounds(10, 10, 75, 15);
		getContentPane().add(label_4);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 35, 374, 9);
		getContentPane().add(separator);

		JLabel lble = new JLabel("OL100\uFF1A");// OL100:
		lble.setFont(new Font("宋体", Font.PLAIN, 12));
		lble.setBounds(10, 106, 75, 18);
		getContentPane().add(lble);

		_OL100_path = new JTextField();
		_OL100_path.setEditable(false);
		_OL100_path.setHorizontalAlignment(SwingConstants.CENTER);
		_OL100_path.setText(Constants.EXCELPATHOL100);
		_OL100_path.setColumns(10);
		_OL100_path.setBounds(87, 105, 192, 21);
		getContentPane().add(_OL100_path);

		JButton btn_OL100 = new JButton("选择");// 选择
		btn_OL100.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// 设定只能选择到文件
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				// 设置文件过滤
				jfc.setFileFilter(new ExcelFileFilter());
				jfc.setCurrentDirectory(new File(Constants.CERTIFICATEPATH));
				// 此句是打开文件选择器界面的触发语句
				int state = jfc.showOpenDialog(null);
				if (state == JFileChooser.CANCEL_OPTION) {
					return;
				} else {
					File file = jfc.getSelectedFile();// file为选择到的文件

					_OL100_path.setText(file.getAbsolutePath());
					// 更新excel路径
					String path = _OL100_path.getText();
					if (!path.isEmpty()) {
						Constants.EXCELPATHOL100 = path;
					}

				}
			}
		});
		btn_OL100.setBounds(289, 104, 85, 23);
		getContentPane().add(btn_OL100);
		// ===============证书编号=================

		JLabel label_2 = new JLabel("\u8BC1\u4E66\u7F16\u53F7\uFF1A");// 证书编号
		label_2.setFont(new Font("宋体", Font.PLAIN, 12));
		label_2.setBounds(10, 44, 65, 15);
		getContentPane().add(label_2);

		certificate_path = new JTextField();
		certificate_path.setEditable(false);
		certificate_path.setHorizontalAlignment(SwingConstants.CENTER);
		certificate_path.setText(Constants.CERTIFICATEPATH);
		certificate_path.setColumns(10);
		certificate_path.setBounds(87, 41, 192, 21);
		getContentPane().add(certificate_path);

		JButton btn_certificate = new JButton("选择");// 选择
		btn_certificate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// 设定只能选择到文件
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				// 设置文件过滤
				jfc.setFileFilter(new ExcelFileFilter());
				jfc.setCurrentDirectory(new File(Constants.CERTIFICATEPATH));
				// 此句是打开文件选择器界面的触发语句
				int state = jfc.showOpenDialog(null);
				if (state == JFileChooser.CANCEL_OPTION) {
					return;
				} else {
					File file = jfc.getSelectedFile();// file为选择到的文件

					certificate_path.setText(file.getAbsolutePath());
					// 更新excel路径
					String path = certificate_path.getText();
					if (!path.isEmpty()) {
						Constants.CERTIFICATEPATH = path;
					}

				}
			}
		});
		btn_certificate.setBounds(289, 40, 85, 23);
		getContentPane().add(btn_certificate);

		JButton btn_rule = new JButton("选择");// 选择
		btn_rule.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// 设定只能选择到文件
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				// 设置文件过滤
				jfc.setFileFilter(new ExcelFileFilter());
				jfc.setCurrentDirectory(new File(Constants.CERTIFICATEPATH));
				// 此句是打开文件选择器界面的触发语句
				int state = jfc.showOpenDialog(null);
				if (state == JFileChooser.CANCEL_OPTION) {
					return;
				} else {
					File file = jfc.getSelectedFile();// file为选择到的文件

					rule_path.setText(file.getAbsolutePath());
					// 更新excel路径
					String path = rule_path.getText();
					if (!path.isEmpty()) {
						Constants.RULEPATH = path;
					}

				}
			}
		});
		btn_rule.setBounds(289, 73, 85, 23);
		getContentPane().add(btn_rule);

		rule_path = new JTextField();
		rule_path.setEditable(false);
		rule_path.setHorizontalAlignment(SwingConstants.CENTER);
		rule_path.setText(Constants.RULEPATH);
		rule_path.setColumns(10);
		rule_path.setBounds(87, 75, 192, 21);
		getContentPane().add(rule_path);

		JLabel label_3 = new JLabel("\u6253\u5370\u89C4\u5219\uFF1A");// 打印规则：
		label_3.setFont(new Font("宋体", Font.PLAIN, 12));
		label_3.setBounds(10, 69, 65, 27);
		getContentPane().add(label_3);

		JLabel lblOl = new JLabel("OL35\uFF1A");// OL35:
		lblOl.setFont(new Font("宋体", Font.PLAIN, 12));
		lblOl.setBounds(10, 135, 65, 18);
		getContentPane().add(lblOl);

		JLabel lblOl_1 = new JLabel("OL25\uFF1A");// OL25:
		lblOl_1.setFont(new Font("宋体", Font.PLAIN, 12));
		lblOl_1.setBounds(10, 163, 75, 18);
		getContentPane().add(lblOl_1);

		JLabel lblOl_SGB = new JLabel("迅达SGB：");// SGB:
		lblOl_SGB.setFont(new Font("宋体", Font.PLAIN, 12));
		lblOl_SGB.setBounds(10, 193, 75, 18);
		getContentPane().add(lblOl_SGB);

		JLabel lblOl_SGB05 = new JLabel("\u901A\u529B\u5B89\u5168\u94B3\uFF1A");// SGB05:
		lblOl_SGB05.setFont(new Font("宋体", Font.PLAIN, 12));
		lblOl_SGB05.setBounds(10, 223, 75, 18);
		getContentPane().add(lblOl_SGB05);

		JLabel lblOl_OL35SCHINDLER = new JLabel("\u8FC5\u8FBEOL35\uFF1A");// OL35SCHINDLER:
		lblOl_OL35SCHINDLER.setFont(new Font("宋体", Font.PLAIN, 12));
		lblOl_OL35SCHINDLER.setBounds(10, 253, 75, 18);
		getContentPane().add(lblOl_OL35SCHINDLER);

		_OL35_path = new JTextField();
		_OL35_path.setText(Constants.EXCELPATHOL35);
		_OL35_path.setHorizontalAlignment(SwingConstants.CENTER);
		_OL35_path.setEditable(false);
		_OL35_path.setColumns(10);
		_OL35_path.setBounds(87, 134, 192, 21);
		getContentPane().add(_OL35_path);

		_OL25_path = new JTextField();
		_OL25_path.setText(Constants.EXCELPATHOL25);
		_OL25_path.setHorizontalAlignment(SwingConstants.CENTER);
		_OL25_path.setEditable(false);
		_OL25_path.setColumns(10);
		_OL25_path.setBounds(87, 162, 192, 21);
		getContentPane().add(_OL25_path);

		_SGB_path = new JTextField();
		_SGB_path.setText(Constants.EXCELPATHSGB);
		_SGB_path.setHorizontalAlignment(SwingConstants.CENTER);
		_SGB_path.setEditable(false);
		_SGB_path.setColumns(10);
		_SGB_path.setBounds(87, 194, 192, 21);
		getContentPane().add(_SGB_path);

		_SGB05_path = new JTextField();
		_SGB05_path.setText(Constants.EXCELPATHSGB05);
		_SGB05_path.setHorizontalAlignment(SwingConstants.CENTER);
		_SGB05_path.setEditable(false);
		_SGB05_path.setColumns(10);
		_SGB05_path.setBounds(87, 222, 192, 21);
		getContentPane().add(_SGB05_path);

		_OL35SCHINDLER_path = new JTextField();
		_OL35SCHINDLER_path.setText(Constants.EXCELPATHOL35SCHINDLER);
		_OL35SCHINDLER_path.setHorizontalAlignment(SwingConstants.CENTER);
		_OL35SCHINDLER_path.setEditable(false);
		_OL35SCHINDLER_path.setColumns(10);
		_OL35SCHINDLER_path.setBounds(87, 250, 192, 21);
		getContentPane().add(_OL35SCHINDLER_path);
		//
		JButton btn_OL35 = new JButton("选择");// 选择
		btn_OL35.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// 设定只能选择到文件
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				// 设置文件过滤
				jfc.setFileFilter(new ExcelFileFilter());
				jfc.setCurrentDirectory(new File(Constants.CERTIFICATEPATH));
				// 此句是打开文件选择器界面的触发语句
				int state = jfc.showOpenDialog(null);
				if (state == JFileChooser.CANCEL_OPTION) {
					return;
				} else {
					File file = jfc.getSelectedFile();// file为选择到的文件

					_OL35_path.setText(file.getAbsolutePath());
					// 更新excel路径
					String path = _OL35_path.getText();
					if (!path.isEmpty()) {
						Constants.EXCELPATHOL35 = path;
					}

				}
			}
		});
		btn_OL35.setBounds(289, 133, 85, 23);
		getContentPane().add(btn_OL35);

		JButton btn_OL25 = new JButton("选择");
		btn_OL25.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// 设定只能选择到文件
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				// 设置文件过滤
				jfc.setFileFilter(new ExcelFileFilter());
				jfc.setCurrentDirectory(new File(Constants.CERTIFICATEPATH));
				// 此句是打开文件选择器界面的触发语句
				int state = jfc.showOpenDialog(null);
				if (state == JFileChooser.CANCEL_OPTION) {
					return;
				} else {
					File file = jfc.getSelectedFile();// file为选择到的文件

					_OL25_path.setText(file.getAbsolutePath());
					// 更新excel路径
					String path = _OL25_path.getText();
					if (!path.isEmpty()) {
						Constants.EXCELPATHOL25 = path;
					}

				}
			}
		});
		btn_OL25.setBounds(289, 163, 85, 23);
		getContentPane().add(btn_OL25);
		/*
		 * 选择文件
		 */
		JButton btn_SGB = new JButton("选择");// SGB选择
		btn_SGB.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// 设定只能选择到文件
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				// 设置文件过滤
				jfc.setFileFilter(new ExcelFileFilter());
				jfc.setCurrentDirectory(new File(Constants.CERTIFICATEPATH));
				// 此句是打开文件选择器界面的触发语句
				int state = jfc.showOpenDialog(null);
				if (state == JFileChooser.CANCEL_OPTION) {
					return;
				} else {
					File file = jfc.getSelectedFile();// file为选择到的文件

					_SGB_path.setText(file.getAbsolutePath());
					// 更新excel路径
					String path = _SGB_path.getText();
					if (!path.isEmpty()) {
						Constants.EXCELPATHSGB = path;
					}

				}
			}
		});
		btn_SGB.setBounds(289, 193, 85, 23);
		getContentPane().add(btn_SGB);

		JButton btn_SGB05 = new JButton("选择");// SGB05选择
		btn_SGB05.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// 设定只能选择到文件
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				// 设置文件过滤
				jfc.setFileFilter(new ExcelFileFilter());
				jfc.setCurrentDirectory(new File(Constants.CERTIFICATEPATH));
				// 此句是打开文件选择器界面的触发语句
				int state = jfc.showOpenDialog(null);
				if (state == JFileChooser.CANCEL_OPTION) {
					return;
				} else {
					File file = jfc.getSelectedFile();// file为选择到的文件

					_SGB05_path.setText(file.getAbsolutePath());
					// 更新excel路径
					String path = _SGB05_path.getText();
					if (!path.isEmpty()) {
						Constants.EXCELPATHSGB05 = path;
					}

				}
			}
		});
		btn_SGB05.setBounds(289, 223, 85, 23);
		getContentPane().add(btn_SGB05);

		JButton btn_OL35SCHINDLER = new JButton("选择");// OL35SCHINDLER选择
		btn_OL35SCHINDLER.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// 设定只能选择到文件
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				// 设置文件过滤
				jfc.setFileFilter(new ExcelFileFilter());
				jfc.setCurrentDirectory(new File(Constants.CERTIFICATEPATH));
				// 此句是打开文件选择器界面的触发语句
				int state = jfc.showOpenDialog(null);
				if (state == JFileChooser.CANCEL_OPTION) {
					return;
				} else {
					File file = jfc.getSelectedFile();// file为选择到的文件

					_OL35SCHINDLER_path.setText(file.getAbsolutePath());
					// 更新excel路径
					String path = _OL35SCHINDLER_path.getText();
					if (!path.isEmpty()) {
						Constants.EXCELPATHOL35SCHINDLER = path;
					}

				}
			}
		});
		btn_OL35SCHINDLER.setBounds(289, 250, 85, 23);
		getContentPane().add(btn_OL35SCHINDLER);

	}

	/**
	 * 更新标签字体大小。
	 */
	// private static void updateFontSize() {
	// SqliteDao sd = new SqliteDao();
	// String size = sd.getFontSize();
	// fontSize.setText(size);
	// }

	/**
	 * 更新标签位置信息。
	 */
	private static void updatePosition() {
		SqliteDao sd = new SqliteDao();
		_OL35SCHINDLER_path.setText(sd.getPath("OL35SCHINDLER"));
		_OL100_path.setText(sd.getPath("OL100"));
		_SGB05_path.setText(sd.getPath("SGB05"));
		_OL35_path.setText(sd.getPath("OL35"));
		_OL25_path.setText(sd.getPath("OL25"));
		_SGB_path.setText(sd.getPath("SGB"));
		rule_path.setText(sd.getPath("rule"));
		certificate_path.setText(sd.getPath("certificate"));
	}
}
